using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Petra.Gis.Geometry;
using Petra.Gis.Geometry.Support;
using Petra.Gis.DataServices.WKB.Support;

namespace Petra.Gis.DataServices.WKB
{
  public partial class GeometryFactory
  {
    /***********************************************
     * 28.8.2007
     * 
     * creates linestringGeometry
     * from WKB blob structure
     * 
     * asida@centrum.cz, initial implementation
     *************************************************/
    private static GeometryBase binaryToLinestring(BinaryReader mBReader/*, int arrayOffset*/, System.Type parentGeomType)
    {
      //read data endian first
      //mBReader.BaseStream.Position = arrayOffset;
      WKB.ByteOrder dataEndian = (WKB.ByteOrder)mBReader.ReadByte();

      //advance stream position to first linestring coordinate (skip geometry type)
      mBReader.BaseStream.Position += 4;

      CoordinateList Coords = CoordinateListConverter.ToCoordinateList(mBReader/*, arrayOffset + 5*/, Dimension.dim2D, dataEndian);
      LineString oGeom = new LineString();
      oGeom.coordList = Coords;

      return oGeom;
    }




    /***********************************************
    * 28.8.2007
    * 
    * creates WKB blob structure 
    * from linestringGeometry
    * 
    * asida@centrum.cz, initial implementation
    *************************************************/
    private static byte[] lineStringToBinary(GeometryBase geometry)
    {
      int lBlobSize;

      LineString _linestring = (LineString)geometry;

      lBlobSize = 1 + 4 + 4 + (_linestring.coordList.Count * 16);

      System.IO.MemoryStream oIMStream = new System.IO.MemoryStream(lBlobSize);
      System.IO.BinaryWriter mBWriter = new System.IO.BinaryWriter(oIMStream);

      oIMStream.Position = 0;

      mBWriter.Write((byte)EndianConverter.GetCurrentPlatformEndian());
      mBWriter.Write((Int32)WKB.WKBGeometryTypes.LineString);
      mBWriter.Write((Int32)_linestring.coordList.Count);
      mBWriter.Write(_linestring.coordList.ToArray(Dimension.dim2D));

      return oIMStream.ToArray();
    }

  }
}
